# 1.准备数据
# 2.准备模型
# 3.模型训练
# 4.模型评估
# 5.模型预测 
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import median_absolute_error,r2_score,max_error,mean_squared_error
from 线性回归模型的类封装 import LinearRegression
from sklearn.preprocessing import StandardScaler

# from sklearn.metrics import r2_score
# from sklearn.metrics import max_error


data = pd.read_excel('home.xlsx')
# print(data.info())                        #查看是否存在缺失值
data = data.dropna(how='any')      #利用dropna删除空白值所在行，any为删除存在空白值行
# X_imp
# X["total_bedrooms"].fillna(X['total_bedrooms'].median,inplace=True)    #用中值填充空白值
# print(data.info())
# data.to_excel('home1.xlsx',sheet_name = 'home')

ss = StandardScaler()


#特征矩阵分为两个
y = data['median_house_value']
X = data.drop("median_house_value",axis = 1)      #扔最后一列，axis = 1为按列扔
X = np.array(X)
y = np.array(y)

lr = LinearRegression()

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15)
 
ss.fit(X_train)
X_train = ss.transform(X_train)
X_test = ss.transform(X_test)

lr.fit(X_train,y_train)

print(mean_squared_error(lr.predict(X_test), y_test))